<template>
  <div>

    <!--面包屑导航区域-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/Overview' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>访问类型表</el-breadcrumb-item>
    </el-breadcrumb>

    <!--    -->
    <el-card class="title">
      <h1>访问类型表</h1>
    </el-card>

    <!--卡片视图区域-->
    <el-card>
      <!--搜索与添加区域-->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getUserList">
            <el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisble = true">添加访问类型</el-button>
        </el-col>
      </el-row>

      <!--用户列表区域-->
      <el-table :data="userlist" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="id" prop="id"></el-table-column>
        <el-table-column label="typeName" prop="typeName"></el-table-column>
        <el-table-column label="siteName" prop="siteName"></el-table-column>
        <el-table-column label="ip" prop="ip"></el-table-column>
        <el-table-column label="port" prop="port"></el-table-column>
        <el-table-column label="操作" width="120px">
          <template slot-scope="scope">
            <!--修改按钮-->
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.id)"></el-button>
            <!--删除按钮-->
            <el-button type="danger" icon="el-icon-delete" size="mini"
                       @click="removeUserById(scope.row.id)"></el-button>

          </template>
        </el-table-column>
      </el-table>

      <!--分页区域-->
      <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="queryInfo.pagenum"
              :page-sizes="[1, 2, 5, 10]"
              :page-size="queryInfo.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
      </el-pagination>

      <!--添加用户的对话框-->
      <el-dialog
              @click="addDialogClosed"
              title="提示"
              :visible.sync="addDialogVisble"
              width="50%">
        <!-- 内容主体区域-->
        <el-form :model="addForm"  ref="addFormRef" label-width="100px">
          <el-form-item label="typeName" >
            <el-input v-model="addForm.typeName"></el-input>
          </el-form-item>
          <el-form-item label="siteName" >
            <el-input v-model="addForm.siteName"></el-input>
          </el-form-item>
          <el-form-item label="ip" >
            <el-input v-model="addForm.ip"></el-input>
          </el-form-item>
          <el-form-item label="port">
            <el-input v-model="addForm.port"></el-input>
          </el-form-item>
        </el-form>
        <!--底部区域-->
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisble = false">取 消</el-button>
          <el-button type="primary" @click="addAccessType">确 定</el-button>
        </span>
      </el-dialog>

      <!--修改用户的对话框-->
      <el-dialog
              @close="editDialogClosed"
              title="提示"
              :visible.sync="editDialogVisible"
              width="50%">
        <el-form :model="editForm" ref="editFormRef" label-width="100px">
          <el-form-item label="id">
            <el-input v-model="editForm.id" disabled></el-input>
          </el-form-item>
          <el-form-item label="typeName" >
            <el-input v-model="editForm.typeName"></el-input>
          </el-form-item>
          <el-form-item label="siteName" >
            <el-input v-model="editForm.siteName"></el-input>
          </el-form-item>
          <el-form-item label="ip" >
            <el-input v-model="editForm.ip"></el-input>
          </el-form-item>
          <el-form-item label="port" >
            <el-input v-model="editForm.port"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editUserInfo">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>

</template>

<script>
import FlowChartDataHandle from '../../DataHandle/FlowChartDataHandle'

export default {
  name: 'AdminTable',
  data () {
    return {
      // 获取用户列表的参数对象
      queryInfo: {
        query:'',
        // 当前的页数
        pagenum: 1,
        // 当前每页显示多少条数据
        pagesize: 5
      },
      userlist: null,
      total: 3,
      // 控制添加用户对话框的显示与隐藏
      addDialogVisble: false,
      // 添加用户的表单数据
      addForm: {
        typeName: '',
        siteName: '',
        ip: '',
        port: ''
      },
      // 控制编辑用户对话框的显示与隐藏
      editDialogVisible: false,
      // 查询到的用户信息对象
      editForm: {},
      // 查询
      query:{
        typeName:'',
        // 当前的页数
        pagenum: 1,
        // 当前每页显示多少条数据
        pagesize: 5
      }
    }
  },
  created () {
    this.getUserList()
  },
  methods: {
    async getUserList () {
      let params = new URLSearchParams()
      params.append('query', this.queryInfo.query)
      params.append('pagenum', this.queryInfo.pagenum - 1)
      params.append('pagesize', this.queryInfo.pagesize)
      let getCapPackClassifyForDateRes = await this.$http.post(
        '/accessType/AccessTypeFind',
        params
      )
      if (getCapPackClassifyForDateRes.data.status !== 200) {
        return this.$message.error('添加失败！')
      }
      this.userlist = getCapPackClassifyForDateRes.data.accessType
      console.log(getCapPackClassifyForDateRes.data)
      // console.log(this.userlist)
      // console.log(getCapPackClassifyForDateRes.data.totalNum)
      this.total = parseInt(getCapPackClassifyForDateRes.data.totalNum)
    },
    // 监听 pagesize 改变事件
    handleSizeChange (newSize) {
      console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getUserList()
    },
    // 监听页码值改变的事件
    handleCurrentChange (newPage) {
      console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getUserList()
    },
    // 监听添加用户对话框的关闭事件
    addDialogClosed () {
      this.$refs.addFormRef.resetFields()
    },
    // 点击按钮 添加用户
    async addAccessType () {
      let params = new URLSearchParams()
      params.append('id', 0)
      params.append('typeName', this.addForm.typeName)
      params.append('siteName', this.addForm.siteName)
      params.append('ip', this.addForm.ip)
      params.append('port', this.addForm.port)
      console.log(params)
      console.log(this.addForm)
      let getCapPackClassifyForDateRes = await this.$http.post(
        '/accessType/insertAccessType',
        params
      )
       console.log(getCapPackClassifyForDateRes)
        if (getCapPackClassifyForDateRes.data.status !== 200) {
          return this.$message.error('添加失败！')
        }
        this.$message.success('添加成功')
        // 隐藏添加用户的对话框
        this.addDialogVisble = false
        // 重新获取用户列表
        this.getUserList()
    },
    // 展示用户编辑的对话框
    async showEditDialog (id) {
      let params = new URLSearchParams()
      params.append('id',id)
      let getCapPackClassifyForDateRes = await this.$http.post(
        '/accessType/findOneAccessTypeById',
        params
      )
      console.log(getCapPackClassifyForDateRes)
      if (getCapPackClassifyForDateRes.data.status !== 200) {
        return this.$message.error('查询用户信息失败！')
      }
      console.log(getCapPackClassifyForDateRes.data)
      this.editForm = getCapPackClassifyForDateRes.data.accessType[0]
      this.editDialogVisible = true
    },
    // 监听修改用户对话框的关闭事件
    editDialogClosed () {
      this.$refs.editFormRef.resetFields()
    },
    // 修改用户信息并提交
    async editUserInfo () {
      console.log(this.editForm.id)
        let params = new URLSearchParams()
        params.append('id',this.editForm.id)
        params.append('typeName', this.editForm.typeName)
        params.append('siteName', this.editForm.siteName)
        params.append('ip', this.editForm.ip)
        params.append('port', this.editForm.port)
        console.log(params)
        console.log(this.addForm)
        let getCapPackClassifyForDateRes = await this.$http.post(
          '/accessType/updateAccessType',
          params
        )
        console.log(getCapPackClassifyForDateRes)
        if (getCapPackClassifyForDateRes.data.status !== 200) {
          return this.$message.error('更新失败！')
        }
        // 关闭对话框
        this.editDialogVisible = false
        // 刷新数据列表
        this.getUserList()
        // 提示修改成功
        this.$message.success('更新信息成功')
    },
    // 根据Id删除对应的用户信息
    async removeUserById (id) {
      // console.log(id)
      // 询问用户是否删除
      const confirmResult = await this.$confirm('此操作将永久删除该条信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async (success) => {
        let params = new URLSearchParams()
        params.append('id',id)
        let getCapPackClassifyForDateRes = await this.$http.post(
          '/accessType/delAccessType',
          params
        )
        console.log(getCapPackClassifyForDateRes)
        if (getCapPackClassifyForDateRes.data.status !== 200) {
          return this.$message.error('查询用户信息失败！')
        }

        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getUserList()

        return success
      }).catch((err) => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
        return err
      })
      console.log(confirmResult)
    }
  }
}
</script>

<style lang="less" scoped>

</style>
